﻿#include "FITKCGNSReader.h"
#include "FITK_Interface/FITKInterfaceMesh/FITKUnstructuredFluidMeshVTK.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Interface/FITKInterfaceMesh/FITKUnstructuredMeshVTK.h"
#include "FITK_Interface/FITKInterfaceModel/FITKElementFactory.h"
#include "FITK_Interface/FITKInterfaceModel/FITKAbstractElement.h"
#include <QDebug>

namespace IO
{
    FITKCGNSReader::~FITKCGNSReader()
    {
        for (baseDataStruct* data : m_baseDatas)
        {
            if (data) delete data;
        }
    }

    void FITKCGNSReader::run()
    {
        *_resultMark = false;
        m_Mesh = dynamic_cast<Interface::FITKUnstructuredFluidMeshVTK*>(_data);
        if (!m_Mesh)return;

        *_resultMark = read();
        if (m_index_file != -1)
            cg_close(m_index_file);
        if (*_resultMark)
            this->setReadData();
    }

    void FITKCGNSReader::consoleMessage(int level, const QString& str)
    {

    }

    bool FITKCGNSReader::read()
    {
        //检查有效的 CGNS 文件 2:hdf5
        int file_type = 0;
        if (CG_OK != cg_is_cgns(_fileName.toStdString().c_str(), &file_type))return false;
        //打开 CGNS 文件
        if (CG_OK != cg_open(_fileName.toStdString().c_str(), CG_MODE_READ, &m_index_file))return false;
        //获取 文件的CGNS 版本
        float fileVersion = -1;
        if (CG_OK != cg_version(m_index_file, &fileVersion))return false;
        //获取 CGNS文件精度
        int precision = -1;
        if (CG_OK != cg_precision(m_index_file, &precision))return false;
        // 获取文件中的 CGNS 基节点数
        int nbases = 0;
        if (CG_OK != cg_nbases(m_index_file, &nbases))return false;
        for (int iBase = 0; iBase < nbases; ++iBase)
        {
            baseDataStruct* data = new baseDataStruct;
            m_baseDatas.append(data);
            //获取基本信息标识
            m_index_base = iBase + 1;
            //读取族
            int nfamily = 0;
            cg_nfamilies(m_index_file, m_index_base, &nfamily);
            for (int i = 1; i <= nfamily; ++i)
            {
                int nBoco = 0, nGeos = 0;
                char name[64] = { 0 };
                cg_family_read(m_index_file, m_index_base, i, name, &nBoco, &nGeos);

            }
            //获取基本信息
            char base_name[33];
            int cell_dim = -1, phys_dim = -1;
            if (CG_OK != cg_base_read(m_index_file, m_index_base, base_name, &cell_dim, &phys_dim))return false;
            //获取 base 中的区域数
            int nzones = 0;
            if (CG_OK != cg_nzones(m_index_file, m_index_base, &nzones))return false;
            //获取区域数据
            for (int iZone = 0; iZone < nzones; ++iZone)
            {
                m_index_zone = iZone + 1;
                if (!readZone())
                    return false;
                //获取单元数据
                if (!this->readSections())
                    return false;
            }
            
        }
        return true;
    }

    bool FITKCGNSReader::readZone()
    {
        //获取区域名称
        char zone_name[33] = { 0 };
        /*int*/ cgsize_t zone_ijk[3][3] = { 0 };
        ///< MG get Zone_t node 's name and every direction vertex cell number
        if (CG_OK != cg_zone_read(m_index_file, m_index_base, m_index_zone, zone_name, zone_ijk[0]))
            return false;
        //获取 CGNS 区域的索引维度
        int dim = 3;
        if (CG_OK != cg_index_dim(m_index_file, m_index_base, m_index_zone, &dim))
            return false;
        //获取节点数量
        int vertex_number = 0;
        /*int*/ cgsize_t read_range[2][3] = { 0 };
        cgsize_t rmax_cell[3]{};
        int cellNumber = 0;
        //获取区域类型（结构化或非结构化）
        CGNS_ENUMT(ZoneType_t) zonetype;
        if (CG_OK != cg_zone_type(m_index_file, m_index_base, m_index_zone, &zonetype)) return false;
        if (zonetype == CGNS_ENUMV(Structured))
        {
            //结构
            for (int n = 0; n < 3; n++)
            {
                read_range[0][n] = 1;
                read_range[1][n] = zone_ijk[0][n];
                rmax_cell[n] = zone_ijk[1][n];
            }
            if (dim == 2)
            {
                read_range[1][2] = 1;
                zone_ijk[0][2] = 1;
            }
            vertex_number = zone_ijk[0][0] * zone_ijk[0][1] * zone_ijk[0][2];
            cellNumber = (zone_ijk[0][0] - 1? zone_ijk[0][0] - 1 : 1) * (zone_ijk[0][1] - 1 ? zone_ijk[0][1] - 1 : 1) * (zone_ijk[0][2] - 1 ? zone_ijk[0][2] - 1 : 1);
        }
        else if (zonetype == CGNS_ENUMV(Unstructured))
        {
            //非结构
            vertex_number = zone_ijk[0][0];
            for (int ijk = 0; ijk < 3; ++ijk)
            {
                read_range[0][ijk] = 1;
                read_range[1][ijk] = vertex_number;
                rmax_cell[ijk] = zone_ijk[0][1];
            }
        }
        else
            return false;
        //读取节点数据
        if (!this->readCoordinate(vertex_number, read_range[0], read_range[1]) || m_baseDatas[m_index_base - 1]->m_verlist.size() != vertex_number)
            return false;

        if (!readZoneFlowSolution(vertex_number, cellNumber, read_range[1], rmax_cell))
            return false;
        // Read Boundary Condition Count
        int bcCount = 0;
        if (CG_OK != cg_nbocos(m_index_file, m_index_base, m_index_zone, &bcCount)) return false;
        // Read Boundary Conditions' Info
        for (int iBc = 1; iBc <= bcCount; ++iBc)
        {
            if (!readBoundaryCond(iBc))
                return false;
        }
        return true;
    }

    bool FITKCGNSReader::readCoordinate(int vertext_num, /*int*/ cgsize_t range_from[3], /*int*/ cgsize_t range_to[3])
    {
        if (m_baseDatas.size() < m_index_base)
            return false;
        //开辟数据空间
        m_baseDatas[m_index_base - 1]->m_verlist.fill(VertexDataInfo(), vertext_num);
        
        //获取方向轴数量
        int coordinate_number = 0;
        if (CG_OK != cg_ncoords(m_index_file, m_index_base, m_index_zone, &coordinate_number))
            return false;
        //获取节点坐标
        for (int coor_index = 1; coor_index <= coordinate_number; ++coor_index)
        {
            //获取方向轴信息
            char coordinate_name[33] = { 0 };
            CGNS_ENUMV(DataType_t) datatype;
            if (CG_OK != cg_coord_info(m_index_file, m_index_base, m_index_zone, coor_index, &datatype, coordinate_name))
                return false;
            //获取坐标轴参数
            double *xyz = new double[vertext_num];
            if (CG_OK != cg_coord_read(m_index_file, m_index_base, m_index_zone, coordinate_name, CGNS_ENUMV(RealDouble), range_from, range_to, xyz))
            {
                delete[] xyz;
                return false;
            }

            if (!strcmp(coordinate_name, "CoordinateX"))
            {
                for (int ipt = 0; ipt < vertext_num; ++ipt)
                {
                    m_baseDatas[m_index_base - 1]->m_verlist[ipt].x = xyz[ipt];
                }
            }
            else if (!strcmp(coordinate_name, "CoordinateY"))
            {
                for (int ipt = 0; ipt < vertext_num; ++ipt)
                {
                    m_baseDatas[m_index_base - 1]->m_verlist[ipt].y = xyz[ipt];
                }
            }
            else if (!strcmp(coordinate_name, "CoordinateZ"))
            {
                for (int ipt = 0; ipt < vertext_num; ++ipt)
                {
                    m_baseDatas[m_index_base - 1]->m_verlist[ipt].z = xyz[ipt];
                }
            }
            delete[] xyz;
        }

        return true;
    }

    bool FITKCGNSReader::readZoneFlowSolution(int pointLen, int cellLen, cgsize_t* rmax_pt, cgsize_t* rmax_cell)
    {
        if (m_baseDatas.size() < m_index_base)
            return false;
        int solCount = 0;
        if (CG_OK != cg_nsols(m_index_file, m_index_base, m_index_zone, &solCount))
            return false;

        for (int iSol = 1; iSol <= solCount; ++iSol)
        {
            char solName[33]{};
            CGNS_ENUMT(GridLocation_t) location;

            if (CG_OK != cg_sol_info(m_index_file, m_index_base, m_index_zone, iSol, solName, &location))
                return false;

            int fieldCount = 0;
            if (CG_OK != cg_nfields(m_index_file, m_index_base, m_index_zone, iSol, &fieldCount))
                return false;

            for (int iField = 1; iField <= fieldCount; ++iField)
            {
                DataType_t dataType;
                char varName[33]{};

                if (CG_OK != cg_field_info(m_index_file, m_index_base, m_index_zone, iSol, iField, &dataType, varName))
                    return false;

                cgsize_t rmin[3]{ 1, 1, 1 };
                cgsize_t rmax[3]{};
                int len = 0;
                if (location == CGNS_ENUMV(Vertex))
                {
                    len = pointLen;
                    rmax[0] = rmax_pt[0];
                    rmax[1] = rmax_pt[1];
                    rmax[2] = rmax_pt[2];
                }
                else if (location == CGNS_ENUMV(CellCenter))
                {
                    len = cellLen;
                    rmax[0] = rmax_cell[0];
                    rmax[1] = rmax_cell[1];
                    rmax[2] = rmax_cell[2];
                }
                else
                    return false;

                void* var{ nullptr };
                if (dataType == RealSingle)
                {
                    var = new float[len];
                }
                else if (dataType == RealDouble)
                {
                    var = new double[len];
                }
                else
                    return false;

                if (CG_OK != cg_field_read(m_index_file, m_index_base, m_index_zone, iSol, varName, dataType, rmin, rmax, var))
                {
                    delete[] var;
                    return false;
                }

                QVector<double> valueVec;
                if (dataType == RealSingle)
                {
                    float* fVar = static_cast<float*>(var);
                    for (int k = 0; k < len; k++)
                    {
                        valueVec.push_back(fVar[k]);
                    }

                    delete[] fVar;
                }
                else if (dataType == RealDouble)
                {
                    double* dVar = static_cast<double*>(var);
                    for (int k = 0; k < len; k++)
                    {
                        valueVec.push_back(dVar[k]);
                    }

                    delete[] dVar;
                }

                switch (location)
                {
                case Vertex:
                {
                    m_baseDatas[m_index_base - 1]->m_pointScalars.insert(varName, valueVec);
                    break;
                }
                case CellCenter:
                {
                    m_baseDatas[m_index_base - 1]->m_cellScalars.insert(varName, valueVec);
                    break;
                }
                default:
                    return false;
                }
            }
        }
        return true;
    }

    bool FITKCGNSReader::readBoundaryCond(int index_bc)
    {
        if (m_baseDatas.size() < m_index_base)
            return false;
        //获取boundary信息
        char bcName[33]{};
        BCType_t type;
        CGNS_ENUMT(PointSetType_t) ptType;
        cgsize_t eleCount;
        int NormalIndex;
        cgsize_t NormalListSize;
        DataType_t dataType;
        int dataSetCount;

        if (CG_OK != cg_boco_info(m_index_file, m_index_base, m_index_zone, index_bc, bcName, &type,
            &ptType, &eleCount, &NormalIndex, &NormalListSize, &dataType, &dataSetCount))
            return false;
        //获取区域类型
        CGNS_ENUMT(ZoneType_t) zonetype;
        if (CG_OK != cg_zone_type(m_index_file, m_index_base, m_index_zone, &zonetype)) return false;
        
        int bcCount = m_baseDatas[m_index_base - 1]->m_bcs.count();
        m_baseDatas[m_index_base - 1]->m_bcs.push_back(BCDataStruct());
        m_baseDatas[m_index_base - 1]->m_bcs[bcCount].name = bcName;
        m_baseDatas[m_index_base - 1]->m_bcs[bcCount].bcType = ptType;
        m_baseDatas[m_index_base - 1]->m_bcs[bcCount].structType = zonetype;
        bool flag = false;
        if (ptType == CGNS_ENUMV(PointList))
        {
            flag = readZoneBCs(&(m_baseDatas[m_index_base - 1]->m_bcs[bcCount]), index_bc, eleCount);
        }
        else if (ptType == CGNS_ENUMV(PointRange))
        {
            if (zonetype == CGNS_ENUMV(Structured))
            {
                flag = readUnstructZoneBCs(&(m_baseDatas[m_index_base - 1]->m_bcs[bcCount]), index_bc);
            }
            else if (zonetype == CGNS_ENUMV(Unstructured))
            {
                flag = readStructZoneBCs(&(m_baseDatas[m_index_base - 1]->m_bcs[bcCount]), index_bc);
            }
        }

        if (!flag)
        {
            m_baseDatas[m_index_base - 1]->m_bcs.remove(bcCount);
        }

        return flag;
    }

    bool FITKCGNSReader::readZoneBCs(BCDataStruct* bcData, int index_bc, int cellCount)
    {
        cgsize_t *pnts = new cgsize_t[cellCount];
        if (CG_OK != cg_boco_read(m_index_file, m_index_base, m_index_zone, index_bc, pnts, nullptr))
        {
            delete[] pnts;
            return false;
        }

        CGNS_ENUMT(GridLocation_t) gridType;
        if (CG_OK != cg_boco_gridlocation_read(m_index_file, m_index_base, m_index_zone, index_bc, &gridType))
        {
            delete[] pnts;
            return false;
        }

        if (gridType == CGNS_ENUMV(Vertex))
        {
            bcData->dataType = 1;
        }
        else if (gridType == CGNS_ENUMV(CellCenter))
        {
            bcData->dataType = 2;
        }
        else
        {
            delete[] pnts;
            return true;
        }
        //边界id
        for (int i = 0; i < cellCount; i++)
        {
            bcData->ids.push_back(pnts[i] - 1);
        }

        delete[] pnts;
        return true;
    }

    bool FITKCGNSReader::readUnstructZoneBCs(BCDataStruct* bcData, int index_bc)
    {
        cgsize_t *pnts = new cgsize_t[2];
        if (CG_OK != cg_boco_read(m_index_file, m_index_base, m_index_zone, index_bc, pnts, nullptr))
        {
            delete[] pnts;
            return false;
        }

        int min = pnts[0];
        int max = pnts[1];

        CGNS_ENUMT(GridLocation_t) gridType;
        if (CG_OK != cg_boco_gridlocation_read(m_index_file, m_index_base, m_index_zone, index_bc, &gridType))
        {
            delete[] pnts;
            return false;
        }

        if (gridType == Vertex)
        {
            bcData->dataType = 1;
        }
        else if (gridType == CellCenter || gridType == FaceCenter || gridType == IFaceCenter ||
            gridType == JFaceCenter || gridType == KFaceCenter)
        {
            bcData->dataType = 2;
        }
        else
        {
            delete[] pnts;
            return true;
        }
        //边界id
        for (int i = min; i <= max; i++)
        {
            bcData->ids.push_back(pnts[i] - 1);
        }

        delete[] pnts;
        return true;
    }

    bool FITKCGNSReader::readStructZoneBCs(BCDataStruct* bcData, int index_bc)
    {
        cgsize_t range[2][3];
        if (CG_OK != cg_boco_read(m_index_file, m_index_base, m_index_zone, index_bc, range[0], range[1]))
        {
            return false;
        }

        CGNS_ENUMT(GridLocation_t) gridType;
        if (CG_OK != cg_boco_gridlocation_read(m_index_file, m_index_base, m_index_zone, index_bc, &gridType))
        {
            return false;
        }

        bcData->range[0][0] = range[0][0];
        bcData->range[0][1] = range[0][1];
        bcData->range[0][2] = range[0][2];

        bcData->range[1][0] = range[1][0];
        bcData->range[1][1] = range[1][1];
        bcData->range[1][2] = range[1][2];

        if (gridType == Vertex)
        {
            bcData->dataType = 1;
        }
        else if (gridType == CellCenter || gridType == FaceCenter || gridType == IFaceCenter ||
            gridType == JFaceCenter || gridType == KFaceCenter)
        {
            bcData->dataType = 2;
        }
        else
        {
            return true;
        }

        return true;
    }

    bool FITKCGNSReader::readSections()
    {
        if (m_baseDatas.size() < m_index_base)
            return false;
        //获取元素集数量
        int nsections = 0;
        cg_nsections(m_index_file, m_index_base, m_index_zone, &nsections);
        //获取元素
        for (int section_index = 1; section_index <= nsections; section_index++)
        {
            //获取元素信息
            char section_name[33] = { 0 };
            /*int*/ cgsize_t istart = 0, iend = 0;
            int nbndry = 0, iparent_flag = 0;
            CGNS_ENUMT(ElementType_t)
                itype;
            if (cg_section_read(m_index_file, m_index_base, m_index_zone, section_index, section_name, &itype, &istart, &iend, &nbndry, &iparent_flag))
                return false;
            
            //获取元素数量
            /*int*/ cgsize_t element_data_size = 0;
            if (cg_ElementDataSize(m_index_file, m_index_base, m_index_zone, section_index, &element_data_size))
                return false;
            if (element_data_size == 0) continue;

            int size = 4 * (iend - istart + 1);
            cgsize_t *elements = new cgsize_t[element_data_size];
            cgsize_t *element_parent = nullptr;
            if (iparent_flag)
            {
                element_parent = new cgsize_t[size];
            }
            if (cg_elements_read(m_index_file, m_index_base, m_index_zone, section_index, elements, element_parent))
            {
                delete[] elements;
                if (element_parent) delete[] element_parent;
                return false;
            }
            if (cg_poly_elements_read(m_index_file, m_index_base, m_index_zone, section_index, elements, 0, element_parent))
            {
                delete[] elements;
                if (element_parent) delete[] element_parent;
                return false;
            }
            //读取单元数据
            int countSection = m_baseDatas[m_index_base - 1]->m_sections.count();
            m_baseDatas[m_index_base - 1]->m_sections.push_back(SectionDataStruct());
            m_baseDatas[m_index_base - 1]->m_sections[countSection].name = section_name;
            this->readCells(&(m_baseDatas[m_index_base - 1]->m_sections[countSection]), istart, iend, itype, elements, element_data_size);
            delete[] elements;
            if (element_parent)delete[] element_parent;
        }
        return true;
    }
    void FITKCGNSReader::readCells(SectionDataStruct* section, int start_index, int end_index, int type, cgsize_t *elements /*int* elements*/, int element_data_size)
    {
        if (m_baseDatas.size() < m_index_base)
            return;

        if (CGNS_ENUMT(TRI_3) == type) //三角形
        {
            int cell_number = element_data_size / 3;
            assert(0 == element_data_size % 3);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {
                int cId = start_index + iCnt;
                int type = Interface::FITKModelEnum::FITKEleType::Tri3;
                QList<int> nodes;
                nodes.append(elements[iCnt * 3 + 0]);
                nodes.append(elements[iCnt * 3 + 1]);
                nodes.append(elements[iCnt * 3 + 2]);
                m_baseDatas[m_index_base - 1]->m_cellList.push_back(CellDataInfo(type, nodes, cId));
                section->ids.push_back(m_baseDatas[m_index_base - 1]->m_cellList.count());
            }
        }
        else if (CGNS_ENUMT(BAR_2) == type) //二节点梁单元
        {
            Interface::FITKBoundaryMeshVTK* boundary = new Interface::FITKBoundaryMeshVTK(m_Mesh->getFieldMesh());
            int cell_number = element_data_size / 2;
            assert(0 == element_data_size % 2);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {
                int cId = start_index + iCnt;
                int type = Interface::FITKModelEnum::FITKEleType::Line2;
                QList<int> nodes;
                nodes.append(elements[iCnt * 2 + 0]);
                nodes.append(elements[iCnt * 2 + 1]);
                m_baseDatas[m_index_base - 1]->m_cellList.push_back(CellDataInfo(type, nodes, cId));
                section->ids.push_back(m_baseDatas[m_index_base - 1]->m_cellList.count());
            }
        }
        else if (CGNS_ENUMT(TETRA_4) == type) //四节点四面体
        {
            int cell_number = element_data_size / 4;
            assert(0 == element_data_size % 4);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {
                int cId = start_index + iCnt;
                int type = Interface::FITKModelEnum::FITKEleType::Tet4;
                QList<int> nodes;
                nodes.append(elements[iCnt * 4 + 0]);
                nodes.append(elements[iCnt * 4 + 1]);
                nodes.append(elements[iCnt * 4 + 2]);
                nodes.append(elements[iCnt * 4 + 3]);
                m_baseDatas[m_index_base - 1]->m_cellList.push_back(CellDataInfo(type, nodes, cId));
                section->ids.push_back(m_baseDatas[m_index_base - 1]->m_cellList.count());
            }
        }
        else if (CGNS_ENUMT(QUAD_4) == type) //四节点四边形
        {
            int cell_number = element_data_size / 4;
            assert(0 == element_data_size % 4);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {
                int cId = start_index + iCnt;
                int type = Interface::FITKModelEnum::FITKEleType::Quad4;
                QList<int> nodes;
                nodes.append(elements[iCnt * 4 + 0]);
                nodes.append(elements[iCnt * 4 + 1]);
                nodes.append(elements[iCnt * 4 + 2]);
                nodes.append(elements[iCnt * 4 + 3]);
                m_baseDatas[m_index_base - 1]->m_cellList.push_back(CellDataInfo(type, nodes, cId));
                section->ids.push_back(m_baseDatas[m_index_base - 1]->m_cellList.count());
            }
        }
        else if (CGNS_ENUMT(HEXA_8) == type) //八节点六面体
        {
            int cell_number = element_data_size / 8;
            assert(0 == element_data_size % 8);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {
                int cId = start_index + iCnt;
                int type = Interface::FITKModelEnum::FITKEleType::Hex8;
                QList<int> nodes;
                nodes.append(elements[iCnt * 8 + 0]);
                nodes.append(elements[iCnt * 8 + 1]);
                nodes.append(elements[iCnt * 8 + 2]);
                nodes.append(elements[iCnt * 8 + 3]);
                nodes.append(elements[iCnt * 8 + 4]);
                nodes.append(elements[iCnt * 8 + 5]);
                nodes.append(elements[iCnt * 8 + 6]);
                nodes.append(elements[iCnt * 8 + 7]);
                m_baseDatas[m_index_base - 1]->m_cellList.push_back(CellDataInfo(type, nodes, cId));
                section->ids.push_back(m_baseDatas[m_index_base - 1]->m_cellList.count());
            }
        }
        else if (CGNS_ENUMT(PENTA_6) == type) //六节点三棱柱
        {
            int cell_number = element_data_size / 6;
            assert(0 == element_data_size % 6);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {
                int cId = start_index + iCnt;
                int type = Interface::FITKModelEnum::FITKEleType::Wedge6;
                QList<int> nodes;
                nodes.append(elements[iCnt * 6 + 0]);
                nodes.append(elements[iCnt * 6 + 1]);
                nodes.append(elements[iCnt * 6 + 2]);
                nodes.append(elements[iCnt * 6 + 3]);
                nodes.append(elements[iCnt * 6 + 4]);
                nodes.append(elements[iCnt * 6 + 5]);
                m_baseDatas[m_index_base - 1]->m_cellList.push_back(CellDataInfo(type, nodes, cId));
                section->ids.push_back(m_baseDatas[m_index_base - 1]->m_cellList.count());
            }
        }
        else if (CGNS_ENUMV(PYRA_5) == type) //金字塔类型
        {
            int cell_number = element_data_size / 5;
            assert(0 == element_data_size % 5);
            assert(end_index - start_index + 1 == cell_number);

            for (int iCnt = 0; iCnt < cell_number; ++iCnt)
            {

            }
        }
        else if (CGNS_ENUMV(MIXED) == type)
        {
            //			qDebug() << "mix";
            int index = 0;
            while (index < element_data_size)
            {
                int eleType = elements[index];
                if (eleType == CGNS_ENUMT(PENTA_6))
                {
                    /*vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
                    idlist->InsertNextId(elements[index + 1] - 1);
                    idlist->InsertNextId(elements[index + 2] - 1);
                    idlist->InsertNextId(elements[index + 3] - 1);
                    idlist->InsertNextId(elements[index + 4] - 1);
                    idlist->InsertNextId(elements[index + 5] - 1);
                    idlist->InsertNextId(elements[index + 6] - 1);
                    ungird->InsertNextCell(VTK_WEDGE, idlist);*/
                    index += 7;
                }
                else if (eleType == CGNS_ENUMT(HEXA_8))
                {
                    /*vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
                    idlist->InsertNextId(elements[index + 1] - 1);
                    idlist->InsertNextId(elements[index + 2] - 1);
                    idlist->InsertNextId(elements[index + 3] - 1);
                    idlist->InsertNextId(elements[index + 4] - 1);
                    idlist->InsertNextId(elements[index + 5] - 1);
                    idlist->InsertNextId(elements[index + 6] - 1);
                    idlist->InsertNextId(elements[index + 7] - 1);
                    idlist->InsertNextId(elements[index + 8] - 1);
                    ungird->InsertNextCell(VTK_HEXAHEDRON, idlist);*/
                    index += 9;
                }
                else if (eleType == CGNS_ENUMT(TRI_3))
                {
                   /* vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
                    idlist->InsertNextId(elements[index + 1] - 1);
                    idlist->InsertNextId(elements[index + 2] - 1);
                    idlist->InsertNextId(elements[index + 3] - 1);
                    ungird->InsertNextCell(VTK_TRIANGLE, idlist);*/
                    index += 4;
                }
                else if (eleType == CGNS_ENUMT(QUAD_4))
                {
                   /* vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
                    idlist->InsertNextId(elements[index + 1] - 1);
                    idlist->InsertNextId(elements[index + 2] - 1);
                    idlist->InsertNextId(elements[index + 3] - 1);
                    idlist->InsertNextId(elements[index + 4] - 1);
                    ungird->InsertNextCell(VTK_QUAD, idlist);*/
                    index += 5;
                }
                else if (eleType == CGNS_ENUMT(TETRA_4))
                {
                    /*vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
                    idlist->InsertNextId(elements[index + 1] - 1);
                    idlist->InsertNextId(elements[index + 2] - 1);
                    idlist->InsertNextId(elements[index + 3] - 1);
                    idlist->InsertNextId(elements[index + 4] - 1);
                    ungird->InsertNextCell(VTK_TETRA, idlist);*/
                    index += 5;
                }
                else if (eleType == CGNS_ENUMT(BAR_2)) //二节点梁单元
                {
                   /* vtkSmartPointer<vtkIdList> idlist = vtkSmartPointer<vtkIdList>::New();
                    idlist->InsertNextId(elements[index + 1] - 1);
                    idlist->InsertNextId(elements[index + 2] - 1);
                    ungird->InsertNextCell(VTK_LINE, idlist);*/
                    index += 3;
                }
                else
                    index += element_data_size;
            }
        }
        //#endif
    }

    void FITKCGNSReader::setReadData()
    {
        if (!m_Mesh)return;
        //获取流体域网格与边界网格管理器
        Interface::FITKUnstructuredMeshVTK* fieldMesh = m_Mesh->getFieldMesh();
        Interface::FITKBoundaryMeshVTKManager* boundaryMeshMgr = m_Mesh->getBoundaryMeshManager();
        if (!fieldMesh || !boundaryMeshMgr)return;
        for (baseDataStruct* baseData : m_baseDatas)
        {
            if (!baseData)continue;
            //插入节点数据
            int nodeCount = baseData->m_verlist.size();
            for (int i = 0; i < nodeCount; ++i)
            {
                VertexDataInfo v = baseData->m_verlist[i];
                fieldMesh->addNode(v.x, v.y, v.z);
            }
            //设置section数据
            int sectionCount = baseData->m_sections.size();
            for (QVector<SectionDataStruct>::iterator iter = baseData->m_sections.begin(); iter != baseData->m_sections.end(); ++iter)
            {
                QString section_name = iter->name;
                Interface::FITKBoundaryMeshVTK* boundaryMesh = new Interface::FITKBoundaryMeshVTK(fieldMesh);
                boundaryMesh->setDataObjectName(section_name);
                int cCount = iter->ids.size();
                for (int i = 0; i < cCount; ++i)
                {
                    CellDataInfo& cellData = baseData->m_cellList[iter->ids[i] - 1];
                    int cId = cellData.cId;
                    int type = cellData.type;
                    QList<int> pIds = cellData.Points;
                    Interface::FITKAbstractElement* element = Interface::FITKElementFactory::createElement(Interface::FITKModelEnum::FITKEleType(type));
                    element->setEleID(cId);
                    element->setNodeID(pIds);
                    boundaryMesh->appendElement(element);
                }
                boundaryMeshMgr->appendDataObj(boundaryMesh);
            }
            //现在仅读取一个
            break;
        }
        
        
    }
}

